package com.tyd.cams;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.tyd.cams.entity.LoginBean;
import com.tyd.cams.utils.MyCustomDialog;
import com.tyd.cams.utils.SPUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;
import okhttp3.OkHttpClient;

/**
 * 登陆活动
 */
public class LoginActivity extends AppCompatActivity {
    private String usernames, passwords;
    private SharedPreferences sharedPreferences;
    private MyCustomDialog myCustomDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        EditText password = findViewById(R.id.login_password);
        EditText username = findViewById(R.id.login_name);
        Button login_btn = findViewById(R.id.login_btn);
        myCustomDialog = new MyCustomDialog(LoginActivity.this);
        login_btn.setOnClickListener(v -> {
            if (TextUtils.isEmpty(username.getText())) {
                Toast.makeText(LoginActivity.this, "请输入用户名", Toast.LENGTH_SHORT).show();
            } else if (TextUtils.isEmpty(password.getText())) {
                Toast.makeText(LoginActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
            } else {
                usernames = username.getText().toString();
                passwords = password.getText().toString();
                myCustomDialog.show();
                LoginPost(usernames, passwords);
            }
        });

    }

/*    //出现
    public void show(View view) {
        myCustomDialog = new MyCustomDialog(this);
        myCustomDialog.show();
    }

    //隐藏
    public void hidden(View view) {
        if(myCustomDialog == null)return;
        myCustomDialog.dismiss();
    }*/

    /**
     * post请求后台实现登陆
     *
     * @param username
     * @param password
     */
    private void LoginPost(String username, String password) {
        OkHttpUtils.post()
            .url(SPUtils.getIP()+"/mlogin")
            .addParams("username", username)
            .addParams("password", password)
            .build()
            .execute(new StringCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    Log.i("url",SPUtils.getIP()+"/mlogin");
                    myCustomDialog.dismiss();
                    Toast.makeText(LoginActivity.this, "登陆失败服务器超时", Toast.LENGTH_SHORT).show();
                    Log.i("获取的返回信息", "登陆失败服务器超时");
                }

                @Override
                public void onResponse(String response, int id) {
                    Log.i("获取的返回信息", response);
                    Gson son = new Gson();
                    LoginBean loginBean = son.fromJson(response, LoginBean.class);
                    System.out.println(loginBean.toString());
                    Boolean status = loginBean.getStatus();//状态
                    String message = loginBean.getMessage();//服务端返回信息
                    if (status) {

                        Toast.makeText(LoginActivity.this, message, Toast.LENGTH_SHORT).show();
                        sharedPreferences = getSharedPreferences("SP", MODE_PRIVATE);
                        SharedPreferences.Editor editor = sharedPreferences.edit();
                        editor.putBoolean("isLogin", false);
                        editor.putString("token", loginBean.getAuthorization());
                        Log.i("获取token", loginBean.getAuthorization());
                        String userinfoJson = new Gson().toJson(loginBean.getUserInfo());
//                        editor.putString("userInfo", loginBean.getUserInfo().toString());
                        editor.putString("userInfo", userinfoJson);

                        editor.putString("username", loginBean.getUserInfo().getUsername());
                        editor.putString("usertype", String.valueOf(loginBean.getUserInfo().getUsertype()));
                        editor.putString("account", loginBean.getUserInfo().getAccount());
                        editor.commit();
                        myCustomDialog.dismiss();
                        Log.i("获取token", SPUtils.getToken());
                        Log.i("获取getUserInfo", SPUtils.getUserInfo());
                        Log.i("获取getUsername", SPUtils.getUsername());
                        Log.i("获取getUsertype", SPUtils.getUsertype());
                        Log.i("获取account", SPUtils.getaccount());
                        Intent intent = new Intent(LoginActivity.this, MainsActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        LoginActivity.this.startActivity(intent);
                        LoginActivity.this.finish();
                    } else {
                        myCustomDialog.dismiss();
                        Toast.makeText(LoginActivity.this, message, Toast.LENGTH_SHORT).show();


                    }
                }
            });
    }

}